package org.liaohailong.victorlibrary.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.liaohailong.library.widget.wheelview.WheelView;
import org.liaohailong.victorlibrary.R;
import org.liaohailong.victorlibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: liaohailong
 * Date: 2022/8/14
 * Time: 9:15
 * Description: 列表选择器
 **/
public class WheelViewActivity extends BaseActivity {
    public static void show(@NonNull Context context) {
        Intent intent = new Intent(context, WheelViewActivity.class);
        context.startActivity(intent);
    }

    private static final String[] PLANETS
            = new String[]{"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Uranus", "Neptune", "Pluto"};

    private static final String[][] DETAILS = new String[][]{
            {"Red", "Orange", "Yellow", "Green", "Qing", "Blue", "Purple"},
            {"Desktop", "Fans", "Table", "Washing Machine", "Bags"},
            {"Noodles", "BaoZi", "Dumplings", "Rice", "Pork", "Beef"},
    };

    private int secondaryPos;


    private void log(String msg, Object... args) {
        String logMsg = String.format(msg, args);
        Log.i("Victor", logMsg);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_select);

        WheelView firstWv = findViewById(R.id.wv_first);
        WheelView secondWv = findViewById(R.id.wv_second);
        TextView resultTxt = findViewById(R.id.tv_result);

        initWheelView(firstWv, PLANETS, 0, (entity, position) -> {
            // 一级列表选中，初始化二级列表
            String[] detail = DETAILS[position % DETAILS.length];
            initWheelView(secondWv, detail, 2, new WheelView.Callback() {
                @Override
                public void onSelectChanged(@NonNull WheelView.Entity entity, int position) {
                    secondaryPos = position;
                    String title = entity.getTitle();
                    resultTxt.setText(title);
                }
            });
        });
    }

    private void initWheelView(@NonNull WheelView view,
                               @NonNull String[] titles,
                               int position,
                               @NonNull WheelView.Callback callback) {
        List<WheelView.Entity> data = new ArrayList<>();
        for (String title : titles) {
            data.add(new WheelView.Entity(title, title));
        }
        view.show(data, position, callback);
    }

}
